{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "d5ffea4e-8ad0-4c4c-9e0b-a8baf578d61d",
   "metadata": {},
   "source": [
    "# 第八节、数据重塑"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "456ee11a-aabd-4504-87ab-d09df437729c",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "5208453e-83bc-4f98-8415-9ed4002d91ad",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>python</th>\n",
       "      <th>tensorflow</th>\n",
       "      <th>keras</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>A</th>\n",
       "      <td>3</td>\n",
       "      <td>4</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>B</th>\n",
       "      <td>4</td>\n",
       "      <td>4</td>\n",
       "      <td>8</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>C</th>\n",
       "      <td>8</td>\n",
       "      <td>2</td>\n",
       "      <td>5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D</th>\n",
       "      <td>7</td>\n",
       "      <td>4</td>\n",
       "      <td>4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>E</th>\n",
       "      <td>1</td>\n",
       "      <td>9</td>\n",
       "      <td>6</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>F</th>\n",
       "      <td>0</td>\n",
       "      <td>9</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>G</th>\n",
       "      <td>8</td>\n",
       "      <td>5</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>H</th>\n",
       "      <td>0</td>\n",
       "      <td>5</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>I</th>\n",
       "      <td>7</td>\n",
       "      <td>7</td>\n",
       "      <td>8</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>J</th>\n",
       "      <td>4</td>\n",
       "      <td>4</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>K</th>\n",
       "      <td>2</td>\n",
       "      <td>5</td>\n",
       "      <td>8</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   python  tensorflow  keras\n",
       "A       3           4      2\n",
       "B       4           4      8\n",
       "C       8           2      5\n",
       "D       7           4      4\n",
       "E       1           9      6\n",
       "F       0           9      2\n",
       "G       8           5      0\n",
       "H       0           5      0\n",
       "I       7           7      8\n",
       "J       4           4      3\n",
       "K       2           5      8"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df = pd.DataFrame(\n",
    "    data=np.random.randint(0, 10, size=(11, 3)),\n",
    "    index=list('ABCDEFGHIJK'),\n",
    "    columns=['python', 'tensorflow', 'keras']\n",
    ")\n",
    "\n",
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "ac0271bc-d474-4bac-884b-0823b021b32e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "      <th>C</th>\n",
       "      <th>D</th>\n",
       "      <th>E</th>\n",
       "      <th>F</th>\n",
       "      <th>G</th>\n",
       "      <th>H</th>\n",
       "      <th>I</th>\n",
       "      <th>J</th>\n",
       "      <th>K</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>python</th>\n",
       "      <td>3</td>\n",
       "      <td>4</td>\n",
       "      <td>8</td>\n",
       "      <td>7</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>8</td>\n",
       "      <td>0</td>\n",
       "      <td>7</td>\n",
       "      <td>4</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>tensorflow</th>\n",
       "      <td>4</td>\n",
       "      <td>4</td>\n",
       "      <td>2</td>\n",
       "      <td>4</td>\n",
       "      <td>9</td>\n",
       "      <td>9</td>\n",
       "      <td>5</td>\n",
       "      <td>5</td>\n",
       "      <td>7</td>\n",
       "      <td>4</td>\n",
       "      <td>5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>keras</th>\n",
       "      <td>2</td>\n",
       "      <td>8</td>\n",
       "      <td>5</td>\n",
       "      <td>4</td>\n",
       "      <td>6</td>\n",
       "      <td>2</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>8</td>\n",
       "      <td>3</td>\n",
       "      <td>8</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "            A  B  C  D  E  F  G  H  I  J  K\n",
       "python      3  4  8  7  1  0  8  0  7  4  2\n",
       "tensorflow  4  4  2  4  9  9  5  5  7  4  5\n",
       "keras       2  8  5  4  6  2  0  0  8  3  8"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 转置\n",
    "df.T"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "2fdb2dab-8bda-482c-99f9-471c7f284a34",
   "metadata": {},
   "outputs": [],
   "source": [
    "df2 = pd.DataFrame(\n",
    "    data=np.random.randint(0, 100, size=(22, 3)),\n",
    "    index=pd.MultiIndex.from_product(\n",
    "        [list('ABCDEFGHIJK'),['期中', '期末']]\n",
    "    ),\n",
    "    columns=['python', 'tensorflow', 'keras']\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "8d855984-3732-4924-93c6-d8b4f37c85b5",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th>python</th>\n",
       "      <th>tensorflow</th>\n",
       "      <th>keras</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">A</th>\n",
       "      <th>期中</th>\n",
       "      <td>62</td>\n",
       "      <td>9</td>\n",
       "      <td>69</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>期末</th>\n",
       "      <td>47</td>\n",
       "      <td>27</td>\n",
       "      <td>79</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">B</th>\n",
       "      <th>期中</th>\n",
       "      <td>18</td>\n",
       "      <td>15</td>\n",
       "      <td>24</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>期末</th>\n",
       "      <td>55</td>\n",
       "      <td>76</td>\n",
       "      <td>71</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">C</th>\n",
       "      <th>期中</th>\n",
       "      <td>4</td>\n",
       "      <td>94</td>\n",
       "      <td>8</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>期末</th>\n",
       "      <td>21</td>\n",
       "      <td>98</td>\n",
       "      <td>61</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">D</th>\n",
       "      <th>期中</th>\n",
       "      <td>96</td>\n",
       "      <td>76</td>\n",
       "      <td>71</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>期末</th>\n",
       "      <td>46</td>\n",
       "      <td>52</td>\n",
       "      <td>38</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">E</th>\n",
       "      <th>期中</th>\n",
       "      <td>43</td>\n",
       "      <td>72</td>\n",
       "      <td>5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>期末</th>\n",
       "      <td>24</td>\n",
       "      <td>26</td>\n",
       "      <td>94</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">F</th>\n",
       "      <th>期中</th>\n",
       "      <td>7</td>\n",
       "      <td>40</td>\n",
       "      <td>76</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>期末</th>\n",
       "      <td>64</td>\n",
       "      <td>97</td>\n",
       "      <td>42</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">G</th>\n",
       "      <th>期中</th>\n",
       "      <td>13</td>\n",
       "      <td>23</td>\n",
       "      <td>24</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>期末</th>\n",
       "      <td>0</td>\n",
       "      <td>91</td>\n",
       "      <td>27</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">H</th>\n",
       "      <th>期中</th>\n",
       "      <td>70</td>\n",
       "      <td>35</td>\n",
       "      <td>95</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>期末</th>\n",
       "      <td>69</td>\n",
       "      <td>99</td>\n",
       "      <td>84</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">I</th>\n",
       "      <th>期中</th>\n",
       "      <td>75</td>\n",
       "      <td>71</td>\n",
       "      <td>41</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>期末</th>\n",
       "      <td>57</td>\n",
       "      <td>21</td>\n",
       "      <td>17</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">J</th>\n",
       "      <th>期中</th>\n",
       "      <td>89</td>\n",
       "      <td>70</td>\n",
       "      <td>30</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>期末</th>\n",
       "      <td>43</td>\n",
       "      <td>75</td>\n",
       "      <td>37</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">K</th>\n",
       "      <th>期中</th>\n",
       "      <td>19</td>\n",
       "      <td>15</td>\n",
       "      <td>87</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>期末</th>\n",
       "      <td>82</td>\n",
       "      <td>25</td>\n",
       "      <td>94</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "      python  tensorflow  keras\n",
       "A 期中      62           9     69\n",
       "  期末      47          27     79\n",
       "B 期中      18          15     24\n",
       "  期末      55          76     71\n",
       "C 期中       4          94      8\n",
       "  期末      21          98     61\n",
       "D 期中      96          76     71\n",
       "  期末      46          52     38\n",
       "E 期中      43          72      5\n",
       "  期末      24          26     94\n",
       "F 期中       7          40     76\n",
       "  期末      64          97     42\n",
       "G 期中      13          23     24\n",
       "  期末       0          91     27\n",
       "H 期中      70          35     95\n",
       "  期末      69          99     84\n",
       "I 期中      75          71     41\n",
       "  期末      57          21     17\n",
       "J 期中      89          70     30\n",
       "  期末      43          75     37\n",
       "K 期中      19          15     87\n",
       "  期末      82          25     94"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "599be825-8fbe-4150-a799-d8a21d83e1c6",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead tr th {\n",
       "        text-align: left;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr>\n",
       "      <th></th>\n",
       "      <th colspan=\"2\" halign=\"left\">python</th>\n",
       "      <th colspan=\"2\" halign=\"left\">tensorflow</th>\n",
       "      <th colspan=\"2\" halign=\"left\">keras</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th></th>\n",
       "      <th>期中</th>\n",
       "      <th>期末</th>\n",
       "      <th>期中</th>\n",
       "      <th>期末</th>\n",
       "      <th>期中</th>\n",
       "      <th>期末</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>A</th>\n",
       "      <td>62</td>\n",
       "      <td>47</td>\n",
       "      <td>9</td>\n",
       "      <td>27</td>\n",
       "      <td>69</td>\n",
       "      <td>79</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>B</th>\n",
       "      <td>18</td>\n",
       "      <td>55</td>\n",
       "      <td>15</td>\n",
       "      <td>76</td>\n",
       "      <td>24</td>\n",
       "      <td>71</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>C</th>\n",
       "      <td>4</td>\n",
       "      <td>21</td>\n",
       "      <td>94</td>\n",
       "      <td>98</td>\n",
       "      <td>8</td>\n",
       "      <td>61</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D</th>\n",
       "      <td>96</td>\n",
       "      <td>46</td>\n",
       "      <td>76</td>\n",
       "      <td>52</td>\n",
       "      <td>71</td>\n",
       "      <td>38</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>E</th>\n",
       "      <td>43</td>\n",
       "      <td>24</td>\n",
       "      <td>72</td>\n",
       "      <td>26</td>\n",
       "      <td>5</td>\n",
       "      <td>94</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>F</th>\n",
       "      <td>7</td>\n",
       "      <td>64</td>\n",
       "      <td>40</td>\n",
       "      <td>97</td>\n",
       "      <td>76</td>\n",
       "      <td>42</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>G</th>\n",
       "      <td>13</td>\n",
       "      <td>0</td>\n",
       "      <td>23</td>\n",
       "      <td>91</td>\n",
       "      <td>24</td>\n",
       "      <td>27</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>H</th>\n",
       "      <td>70</td>\n",
       "      <td>69</td>\n",
       "      <td>35</td>\n",
       "      <td>99</td>\n",
       "      <td>95</td>\n",
       "      <td>84</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>I</th>\n",
       "      <td>75</td>\n",
       "      <td>57</td>\n",
       "      <td>71</td>\n",
       "      <td>21</td>\n",
       "      <td>41</td>\n",
       "      <td>17</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>J</th>\n",
       "      <td>89</td>\n",
       "      <td>43</td>\n",
       "      <td>70</td>\n",
       "      <td>75</td>\n",
       "      <td>30</td>\n",
       "      <td>37</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>K</th>\n",
       "      <td>19</td>\n",
       "      <td>82</td>\n",
       "      <td>15</td>\n",
       "      <td>25</td>\n",
       "      <td>87</td>\n",
       "      <td>94</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "  python     tensorflow     keras    \n",
       "      期中  期末         期中  期末    期中  期末\n",
       "A     62  47          9  27    69  79\n",
       "B     18  55         15  76    24  71\n",
       "C      4  21         94  98     8  61\n",
       "D     96  46         76  52    71  38\n",
       "E     43  24         72  26     5  94\n",
       "F      7  64         40  97    76  42\n",
       "G     13   0         23  91    24  27\n",
       "H     70  69         35  99    95  84\n",
       "I     75  57         71  21    41  17\n",
       "J     89  43         70  75    30  37\n",
       "K     19  82         15  25    87  94"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 行旋转成列，level指定哪一层进行变换\n",
    "df2.unstack(level=-1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "b8d12524-ee3f-48cb-829e-f694f8270be5",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "A  期中  python        62\n",
       "       tensorflow     9\n",
       "       keras         69\n",
       "   期末  python        47\n",
       "       tensorflow    27\n",
       "                     ..\n",
       "K  期中  tensorflow    15\n",
       "       keras         87\n",
       "   期末  python        82\n",
       "       tensorflow    25\n",
       "       keras         94\n",
       "Length: 66, dtype: int32"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 列旋转成行\n",
    "df2.stack()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "8802f637-9059-469d-91b5-6650674d50cd",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th>期中</th>\n",
       "      <th>期末</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th rowspan=\"3\" valign=\"top\">A</th>\n",
       "      <th>python</th>\n",
       "      <td>62</td>\n",
       "      <td>47</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>tensorflow</th>\n",
       "      <td>9</td>\n",
       "      <td>27</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>keras</th>\n",
       "      <td>69</td>\n",
       "      <td>79</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"3\" valign=\"top\">B</th>\n",
       "      <th>python</th>\n",
       "      <td>18</td>\n",
       "      <td>55</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>tensorflow</th>\n",
       "      <td>15</td>\n",
       "      <td>76</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>keras</th>\n",
       "      <td>24</td>\n",
       "      <td>71</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"3\" valign=\"top\">C</th>\n",
       "      <th>python</th>\n",
       "      <td>4</td>\n",
       "      <td>21</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>tensorflow</th>\n",
       "      <td>94</td>\n",
       "      <td>98</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>keras</th>\n",
       "      <td>8</td>\n",
       "      <td>61</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"3\" valign=\"top\">D</th>\n",
       "      <th>python</th>\n",
       "      <td>96</td>\n",
       "      <td>46</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>tensorflow</th>\n",
       "      <td>76</td>\n",
       "      <td>52</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>keras</th>\n",
       "      <td>71</td>\n",
       "      <td>38</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"3\" valign=\"top\">E</th>\n",
       "      <th>python</th>\n",
       "      <td>43</td>\n",
       "      <td>24</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>tensorflow</th>\n",
       "      <td>72</td>\n",
       "      <td>26</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>keras</th>\n",
       "      <td>5</td>\n",
       "      <td>94</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"3\" valign=\"top\">F</th>\n",
       "      <th>python</th>\n",
       "      <td>7</td>\n",
       "      <td>64</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>tensorflow</th>\n",
       "      <td>40</td>\n",
       "      <td>97</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>keras</th>\n",
       "      <td>76</td>\n",
       "      <td>42</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"3\" valign=\"top\">G</th>\n",
       "      <th>python</th>\n",
       "      <td>13</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>tensorflow</th>\n",
       "      <td>23</td>\n",
       "      <td>91</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>keras</th>\n",
       "      <td>24</td>\n",
       "      <td>27</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"3\" valign=\"top\">H</th>\n",
       "      <th>python</th>\n",
       "      <td>70</td>\n",
       "      <td>69</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>tensorflow</th>\n",
       "      <td>35</td>\n",
       "      <td>99</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>keras</th>\n",
       "      <td>95</td>\n",
       "      <td>84</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"3\" valign=\"top\">I</th>\n",
       "      <th>python</th>\n",
       "      <td>75</td>\n",
       "      <td>57</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>tensorflow</th>\n",
       "      <td>71</td>\n",
       "      <td>21</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>keras</th>\n",
       "      <td>41</td>\n",
       "      <td>17</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"3\" valign=\"top\">J</th>\n",
       "      <th>python</th>\n",
       "      <td>89</td>\n",
       "      <td>43</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>tensorflow</th>\n",
       "      <td>70</td>\n",
       "      <td>75</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>keras</th>\n",
       "      <td>30</td>\n",
       "      <td>37</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"3\" valign=\"top\">K</th>\n",
       "      <th>python</th>\n",
       "      <td>19</td>\n",
       "      <td>82</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>tensorflow</th>\n",
       "      <td>15</td>\n",
       "      <td>25</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>keras</th>\n",
       "      <td>87</td>\n",
       "      <td>94</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "              期中  期末\n",
       "A python      62  47\n",
       "  tensorflow   9  27\n",
       "  keras       69  79\n",
       "B python      18  55\n",
       "  tensorflow  15  76\n",
       "  keras       24  71\n",
       "C python       4  21\n",
       "  tensorflow  94  98\n",
       "  keras        8  61\n",
       "D python      96  46\n",
       "  tensorflow  76  52\n",
       "  keras       71  38\n",
       "E python      43  24\n",
       "  tensorflow  72  26\n",
       "  keras        5  94\n",
       "F python       7  64\n",
       "  tensorflow  40  97\n",
       "  keras       76  42\n",
       "G python      13   0\n",
       "  tensorflow  23  91\n",
       "  keras       24  27\n",
       "H python      70  69\n",
       "  tensorflow  35  99\n",
       "  keras       95  84\n",
       "I python      75  57\n",
       "  tensorflow  71  21\n",
       "  keras       41  17\n",
       "J python      89  43\n",
       "  tensorflow  70  75\n",
       "  keras       30  37\n",
       "K python      19  82\n",
       "  tensorflow  15  25\n",
       "  keras       87  94"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 行列互换\n",
    "df2.stack().unstack(level=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "ffdcac6e-8706-4672-a995-b770555aee6d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "python        45.636364\n",
       "tensorflow    54.863636\n",
       "keras         53.363636\n",
       "dtype: float64"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 多层索引的数学计算\n",
    "df2.mean()  # 各科平均分"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "ea328ffe-6ec3-4aa7-b575-79f8c33c39a6",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "python      A    54.5\n",
       "            B    36.5\n",
       "            C    12.5\n",
       "            D    71.0\n",
       "            E    33.5\n",
       "            F    35.5\n",
       "            G     6.5\n",
       "            H    69.5\n",
       "            I    66.0\n",
       "            J    66.0\n",
       "            K    50.5\n",
       "tensorflow  A    18.0\n",
       "            B    45.5\n",
       "            C    96.0\n",
       "            D    64.0\n",
       "            E    49.0\n",
       "            F    68.5\n",
       "            G    57.0\n",
       "            H    67.0\n",
       "            I    46.0\n",
       "            J    72.5\n",
       "            K    20.0\n",
       "keras       A    74.0\n",
       "            B    47.5\n",
       "            C    34.5\n",
       "            D    54.5\n",
       "            E    49.5\n",
       "            F    59.0\n",
       "            G    25.5\n",
       "            H    89.5\n",
       "            I    29.0\n",
       "            J    33.5\n",
       "            K    90.5\n",
       "dtype: float64"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df2.unstack(level=0).mean()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "c3674b1a-4e8e-4748-b057-189f88166eeb",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "python      期中    45.090909\n",
       "            期末    46.181818\n",
       "tensorflow  期中    47.272727\n",
       "            期末    62.454545\n",
       "keras       期中    48.181818\n",
       "            期末    58.545455\n",
       "dtype: float64"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df2.unstack(level=1).mean()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "bd932481-9838-43dc-ba96-88101fabf6b6",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.11"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
